package com.zzh.takeoutapp.activity;

import android.Manifest;
import android.annotation.SuppressLint;
import android.content.Context;
import android.content.pm.PackageManager;
import android.provider.Settings;
import android.support.v4.app.ActivityCompat;
import android.support.v7.widget.AppCompatEditText;
import android.support.v7.widget.Toolbar;
import android.telephony.TelephonyManager;
import android.text.method.HideReturnsTransformationMethod;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.zzh.takeoutapp.R;
import com.zzh.takeoutapp.base.BaseActivity;
import com.zzh.takeoutapp.config.Constants;
import com.zzh.takeoutapp.event.NetMsgEvent;
import com.zzh.takeoutapp.service.EventbusHelper;
import com.zzh.takeoutapp.service.ServiceUrl;
import com.zzh.takeoutapp.utils.CountTimerUtlis;
import com.zzh.takeoutapp.utils.LoadingUtil;
import com.zzh.takeoutapp.utils.PhoneUtils;
import com.zzh.takeoutapp.utils.SPUtils;
import com.zzh.takeoutapp.utils.ToolBarHelper;
import com.zzh.takeoutapp.utils.UtilTool;

import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;
import org.json.JSONObject;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * Created by Administrator on 2018/8/15.
 */

public class RegisterActivity extends BaseActivity {
    @Bind(R.id.toolbar)
    Toolbar tbToolbar;
    @Bind(R.id.login_phone)
    AppCompatEditText login_phone;
    @Bind(R.id.login_paw)
    AppCompatEditText login_paw;
    @Bind(R.id.login_password)
    AppCompatEditText login_password;
    @Bind(R.id.tv_submit)
    TextView tv_submit;
    @Bind(R.id.tv_verify)
    TextView tv_verify;
    @Bind(R.id.iv_cancel)
    ImageView iv_cancel;
    @Bind(R.id.iv_cancel_password)
    ImageView iv_cancel_password;
    @Bind(R.id.iv_cancel_paw)
    ImageView iv_cancel_paw;

    private CountTimerUtlis countTimerUtlis;
    private LoadingUtil loadingUtil;

    @Override
    public void setContent() {
        setContentView(R.layout.activity_register);
        ButterKnife.bind(this);
        loadingUtil = new LoadingUtil(this);

    }

    private String getStr(AppCompatEditText login_paw) {
        return login_paw.getText().toString().trim();
    }

    private boolean isBool() {
        if (PhoneUtils.phoneVerify(this, getStr(login_phone)) == false) {
            return false;
        } else if (getStr(login_password) == null || getStr(login_password).equals("")) {
            UtilTool.toast(this, "密码不能为空");
            return false;
        } else if (getStr(login_paw) == null || getStr(login_paw).equals("")) {
            UtilTool.toast(this, "验证码不能为空");
            return false;
        }
        return true;
    }

    @Override
    public void initComponent() {
        ToolBarHelper.setTitleAndBackAndText(this, tbToolbar, "注册");
    }

    @Override
    public void setListener() {

    }

    @Override
    public void onAfterInit() {

    }

    @OnClick({R.id.tv_submit, R.id.tv_verify, R.id.iv_cancel, R.id.iv_cancel_password, R.id.iv_cancel_paw})
    @Override
    public void onClick(View view) {
        super.onClick(view);
        switch (view.getId()) {
            case R.id.iv_cancel:
                login_phone.setText("");
                break;
            case R.id.iv_cancel_password:
                login_password.setText("");
                break;
            case R.id.iv_cancel_paw:
                login_paw.setText("");
                break;
            case R.id.tv_submit:
                if (isBool() == true) {
                    loadingUtil.startShowLoading();
                    ServiceUrl.RegisterService(this,
                            getStr(login_phone),
                            getStr(login_password),
                            getStr(login_paw),
                            "REGISTER_SERVICE");
                }
                break;
            case R.id.tv_verify:
                if (PhoneUtils.phoneVerify(this, getStr(login_phone)) == true) {
                    ServiceUrl.sendsmsServiceUrl(this,
                            getStr(login_phone),
                            "1",
                            "SENDSMS_SERVICEURL");

                }
                break;

        }
    }

    @Override
    public void onStart() {
        super.onStart();
        EventbusHelper.register(this);
    }

    @Override
    public void onStop() {
        loadingUtil.stopShowLoading();
        EventbusHelper.unregister(this);
        super.onStop();
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onMsgEvent(NetMsgEvent event) {
        String _msgType = (String) event.getMsgType();
        JSONObject jsonObject = event.getMsgJSONObj();
        switch (_msgType) {
            case "REGISTER_SERVICE":
                loadingUtil.stopShowLoading();
                UtilTool.toast(this, jsonObject.optString("message"));
                if (jsonObject.optString("error").equals("0")) {
                    JSONObject data = jsonObject.optJSONObject("data");
                    SPUtils.saveString(this, Constants.mobile, getStr(login_phone));
                    SPUtils.saveString(this, Constants.passwd, getStr(login_password));
                    SPUtils.saveString(this, Constants.FACE, data.optString("face"));
                    finish_Activity();
                }
                break;
            case "SENDSMS_SERVICEURL":
                UtilTool.toast(this, jsonObject.optString("message"));
                if (jsonObject.optString("error").equals("0")) {
                    countTimerUtlis = new CountTimerUtlis(60 * 1000,
                            1000, tv_verify, R.string.time, new CountTimerUtlis.Call() {
                        @Override
                        public void END() {
                            tv_verify.setTextColor(getResources().getColor(R.color.color_red));
                            countTimerUtlis.cancel();
                        }
                    });
                    countTimerUtlis.start();
                }
                break;
        }
    }
}
